package cheqex.update;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import cheqex.dao.Withdrawals;
import cheqex.dao.WithdrawalsPeer;
import cheqex.dao.Withdrawals;
import cheqex.dao.WithdrawalsPeer;
import cheqex.update.controllers.DateController;

public class WithdrawalsView extends JPanel implements ActionListener{
	
	private JScrollPane scrollPane;
	private JTable table;
	
	private JButton addB;
	private JButton deleteB;
	private JButton editB;
	
	
	public WithdrawalsView(){
		initialize();
	}
	
	public void populateTable(){
		this.table.setModel(new DefaultTableModel(getWData(), getWNames()));
	}
	
	private String[][] getWData(){
		List<Withdrawals> list = WithdrawalsPeer.retrieveAllWithdrawals(DateController.getDate());
		String[][] data = new String[list.size()][3];
		for(int i=0; i<list.size(); i++){
			Withdrawals dc = list.get(i);
			data[i][0] = "" + dc.getCheckNumber();
			data[i][1] = "" + dc.getAmount();
			data[i][2] = "" + dc.getParticulars();
		}
		return data;
	}
	
	
	private String[] getWNames(){
		return new String[]
			{"Check Number", "Amount", "Particulars"};
	}
	
	public void initialize(){
		this.table = new JTable();
		this.table.setFillsViewportHeight(true);
		this.table.setShowGrid(true);
		this.table.setOpaque(true);
	    this.table.setAutoResizeMode(5);
	    this.table.setRowHeight(24);
	    this.table.setSize(20, 50);
	    this.table.setEnabled(false);
		
	    this.scrollPane = new JScrollPane(this.table);
		populateTable();
		this.table.getTableHeader().setReorderingAllowed(false);
		
		addB = new JButton("Add+");
		deleteB = new JButton("Delete-");
		editB = new JButton("Edit");
		
	    //buttons
	    addB.setMnemonic(KeyEvent.VK_D);
	    addB.setActionCommand("add");
	    addB.setEnabled(true);
	    addB.addActionListener(this);
	    
	    editB.setMnemonic(KeyEvent.VK_D);
	    editB.setActionCommand("edit");
	    editB.setEnabled(true);
	    editB.addActionListener(this);
	    
	    deleteB.setMnemonic(KeyEvent.VK_D);
	    deleteB.setActionCommand("delete");
	    deleteB.setEnabled(true);
	    deleteB.addActionListener(this);

        JPanel jp = new JPanel();
        jp.setLayout(new GridLayout(4,0,4,0));
        jp.add(addB);
		jp.add(editB);
		jp.add(deleteB);
		jp.setSize(new Dimension(10,10));
				
		this.add(jp);
		this.add(scrollPane);
		
		this.setAutoscrolls(true);
		this.setEnabled(true);
		this.setLayout(new FlowLayout(FlowLayout.LEFT));
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if ("add".equals(e.getActionCommand())) {
			AddWithdrawalsView add = new AddWithdrawalsView(this);
			add.setVisible(true);
		}
		
		if ("edit".equals(e.getActionCommand())) {
			EditWithdrawalsView edit = new EditWithdrawalsView(this);
			edit.setVisible(true);
		}
		
		if ("delete".equals(e.getActionCommand())) {
			DeleteWithdrawalsView delete = new DeleteWithdrawalsView(this);
			delete.setVisible(true);
		}
		
		
	}
}
